<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class penjualan extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library("phpexcel");
        $this->load->library("PHPExcel/iofactory");

        $this->load->model("report/m_simpanan");
        $this->load->model("crud/m_info_usaha");
    }

    function index() {
        $this->simpanan();
    }

    function simpanan() {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Penjualan Toko');
        $this->load->view('pengurus/br-08/penjualan');
    }

    function date() {
        $start_date = $this->input->post("AWAL_TANGGAL");
        $end_date = $this->input->post("AKHIR_TANGGAL");

        $this->load->model('crud/m_transaksi_kasir');

        $result = $this->m_transaksi_kasir->get("`TGL_TRANSAKSI` >= date('$start_date') and `TGL_TRANSAKSI` <= date('$end_date')");//$this->m_akuntansi->sum_jurnal($date);

        // --------------------------- create excel ---------------------------------//
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("Laporan Penjualan")->setDescription("description");

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);

        $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);

        $objPHPExcel->getActiveSheet()->getStyle('A8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A8'), 'B8:I8' );

        $objPHPExcel->getActiveSheet()->setCellValue('A1', "KOPERASI KARYAWAN TOTAL INDONESIE");
        $objPHPExcel->getActiveSheet()->setCellValue('A2', "JL.JEND.SUDIRMAN H2 NO.5-6 BALIKPAPAN PERMAI");
        $objPHPExcel->getActiveSheet()->setCellValue('A3', "BALIKPAPAN");
        $objPHPExcel->getActiveSheet()->setCellValue('A4', "Tlp.: 0542-427723, Fac.: 0542-427722");
        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->mergeCells('A1:I1');
        $sheet->mergeCells('A2:I2');
        $sheet->mergeCells('A3:I3');
        $sheet->mergeCells('A4:I4');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue('A6', "PENJUALAN : $start_date - $end_date");
        $objPHPExcel->getActiveSheet()->setCellValue('H6', "TANGGAL : ".date('Y-m-d'));


        $objPHPExcel->getActiveSheet()->setCellValue('A8', "No");
        $objPHPExcel->getActiveSheet()->setCellValue('B8', "Tanggal");
        $objPHPExcel->getActiveSheet()->setCellValue('C8', "Nomer Faktur");
        $objPHPExcel->getActiveSheet()->setCellValue('D8', "Kode");
        $objPHPExcel->getActiveSheet()->setCellValue('E8', "Nama");
        $objPHPExcel->getActiveSheet()->setCellValue('F8', "Satuan");
        $objPHPExcel->getActiveSheet()->setCellValue('G8', "Jumlah");
        $objPHPExcel->getActiveSheet()->setCellValue('H8', "Harga / Unit");
        $objPHPExcel->getActiveSheet()->setCellValue('I8', "Nilai Jual");

        $i = 8;
        $no_group = 1;
        $date_group = $faktur_group = "";

        foreach($result->result() as $row) {
            $i++;

            if($date_group != $row->TGL_TRANSAKSI)
            {
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $no_group++);
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $row->TGL_TRANSAKSI);
            }

            if($faktur_group != $row->ID_TRANSAKSI_KASIR)
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $row->ID_TRANSAKSI_KASIR);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $row->ID_BARANG);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $row->NAMA_BARANG);
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $row->NAMA_SATUAN);
            $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $row->JUMLAH);
            $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $row->HARGA_JUAL);
            $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $row->JUMLAH * $row->HARGA_JUAL);

            $date_group = $row->TGL_TRANSAKSI;
            $faktur_group = $row->ID_TRANSAKSI_KASIR;
        }
        $i+=2;
        $objPHPExcel->getActiveSheet()->getStyle("H8$i")->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle("I8$i")->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->setCellValue("H$i", "Grand Total");
        $objPHPExcel->getActiveSheet()->setCellValue("I$i", "=SUM(I9:I".($i - 1).")");
        
        $objPHPExcel->getActiveSheet()->getStyle('H10')->getNumberFormat()->setFormatCode('Rp#,##');
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('H10'), "H10:I$i" );

        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&G&C&HLaporan Penjualan\n!');
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // Save it as an excel 2003 file
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=Laporan_Penjualan_[$start_date~$end_date]");
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }
}
?>